package com.yyhh.helpcall.utils;

import android.content.Context;
import android.text.TextUtils;
import com.huawei.agconnect.config.AGConnectServicesConfig;
import com.huawei.hms.aaid.HmsInstanceId;
import com.yyhh.helpcall.MainApplication;
import com.yyhh.helpcall.http.OkHttpUtils;
import com.yyhh.helpcall.http.bean.BaseResponse;
import com.yyhh.helpcall.http.bean.CallLogResponse;
import java.util.HashMap;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class HuaWeiUtils {

    public static void initHuaWeiToken(Context context){
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                String appId = AGConnectServicesConfig.fromContext(context).getString("client/app_id");
                String token = HmsInstanceId.getInstance(context).getToken(appId, "HCM");
                if(!TextUtils.isEmpty(token)){
                    NetLog.d("initHuaWeiPush result = " + token);
                    e.onNext(token);
                    e.onComplete();
                }else{
                    e.onNext("");
                    e.onComplete();
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String value) {
                        if(!TextUtils.isEmpty(value)){
                            bindHuaWeiToken(value);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public static void bindHuaWeiToken(String token){
        HashMap<String, String> map = new HashMap<>();
        map.put("huawei_token",token);
        map.put("token",SpUtils.getInstance(MainApplication.mainApplication).getString("token"));
        OkHttpUtils.getInstance().getApi().bindHuaWeiToken(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResponse<CallLogResponse>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResponse<CallLogResponse> value) {
                        if(value.isSuccess()){
                            NetLog.d("bindHuaWeiToken result = " + value.toString());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public static void deleteHuaWeiToken(Context context){
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> e) throws Exception {
                String appId = AGConnectServicesConfig.fromContext(context).getString("client/app_id");
                HmsInstanceId.getInstance(context).deleteToken(appId, "HCM");
                e.onNext(1);
                e.onComplete();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Object value) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
